<template>
  <div class="icons">
    <swiper ref="mySwiper" :options="swiperOptions" v-if="showSwiper">
      <swiper-slide v-for="page in pages" :key="page.id">
        <div class="icons__icon" v-for="item in page" :key="item.id">
          <div class="icons__icon__img">
            <img
              :src="item.imgUrl"
              :alt="item.id"
              class="icons__icon__img__content"
            />
          </div>
          <p class="icons__icon__desc">{{ item.desc }}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  name: "HomeIcons",
  props: {
    list: Array,
  },
  data() {
    return {
      swiperOptions: {},
    };
  },
  computed: {
    pages() {
      const pages = [];
      this.list.forEach((item, index) => {
        // 实现分页算法
        const count = Math.floor(index / 8);
        if (!pages[count]) {
          pages[count] = [];
        }
        pages[count].push(item);
      });
      return pages;
    },
    showSwiper() {
      return this.list.length;
    },
  },
};
</script>

<style lang="scss">
@import "~../../../../assets/style/minixs.scss";
.icons ::v-deep .swiper-container {
  height: 0;
  padding-bottom: 50%;
}
.icons {
  margin-top: 0.1rem;
  overflow: hidden;
  &__icon {
    position: relative;
    float: left;
    overflow: hidden;
    width: 25%;
    height: 0;
    padding-bottom: 25%;
    &__img {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0.44rem;
      // 将图片向里面缩小 0.1rem 不会撑宽盒子
      box-sizing: border-box;
      padding: 0.1rem;
      &__content {
        display: block;
        height: 100%;
        margin: 0 auto;
      }
    }
    &__desc {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      height: 0.44rem;
      line-height: 0.44rem;
      text-align: center;
      color: #333;
      @include ellipsis();
    }
  }
}
</style>